package com.dynamixsoftware.printershare;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AbsListView.LayoutParams;
import com.dynamixsoftware.printershare.ActivityCloudPrinters;
import com.dynamixsoftware.printershare.ActivityCore;
import com.dynamixsoftware.printershare.ActivityLocalPrinters;
import com.dynamixsoftware.printershare.ActivityPrinters;
import com.dynamixsoftware.printershare.App;
import com.dynamixsoftware.printershare.data.Printer;

public abstract class ActivityBase extends ActivityCore {

	protected static final int OPTIONS_MENU_PRINTERS = 3;
	protected AlertDialog printers_menu;

	protected void init() {
		if (token == null) {
			super.init();
		}

	}

	public void onCreate(Bundle var1) {
		super.onCreate(var1);
		final Resources val$res = getResources();
		BaseAdapter var2 = new BaseAdapter() {

			private float toPixel(int var1) {
				return TypedValue.applyDimension(1, (float) var1, val$res.getDisplayMetrics());
			}

			public int getCount() {
				return 5;
			}

			public Object getItem(int var1) {
				return Integer.valueOf(var1);
			}

			public long getItemId(int var1) {
				return (long) var1;
			}

			public View getView(int var1, View var2, ViewGroup var3) {
				if (var2 == null) {
					TextView var4 = new TextView(ActivityBase.this);
					var4.setLayoutParams(new LayoutParams(-1, -2));
					var4.setPadding((int) this.toPixel(3), 0, (int) this.toPixel(3), 0);
					var4.setGravity(16);
					Theme var5 = ActivityBase.this.getTheme();
					TypedValue var6 = new TypedValue();
					if (var5.resolveAttribute(16842819, var6, true)) {
						var4.setTextAppearance(ActivityBase.this, var6.resourceId);
					}

					var4.setMinHeight((int) this.toPixel(48));
					var4.setCompoundDrawablePadding((int) this.toPixel(5));
					var2 = var4;
				}

				TextView var7 = (TextView) var2;
				var7.setTag(Integer.valueOf(var1));
				int var8 = 0;
				int var9 = 0;
				switch (var1) {
				case 0:
					var9 = 2131099652;
					var8 = 2130837552;
					break;
				case 1:
					var9 = 2131099653;
					var8 = 2130837550;
					break;
				case 2:
					var9 = 2131099654;
					var8 = 2130837551;
					break;
				case 3:
					var9 = 2131099655;
					var8 = 2130837532;
					break;
				case 4:
					var9 = 2131099656;
					var8 = 2130837563;
				}

				var7.setText(var9);
				var7.setCompoundDrawablesWithIntrinsicBounds(var8, 0, 0, 0);
				var7.setEnabled(this.isEnabled(var1));
				Drawable var10 = var7.getCompoundDrawables()[0];
				short var11;
				if (this.isEnabled(var1)) {
					var11 = 255;
				} else {
					var11 = 128;
				}

				var10.setAlpha(var11);
				return var7;
			}

			public boolean isEnabled(int var1) {
				boolean var2 = true;
				if (var1 == 1) {
					var2 = App.has_feature_bluetooth;
				}

				if (var1 == 2) {
					var2 = App.has_feature_usb;
				}

				return var2;
			}
		};
		this.printers_menu = (new Builder(this)).setTitle(2131099651).setIcon(2130837549)
				.setAdapter(var2, new OnClickListener() {
					public void onClick(DialogInterface var1, int var2) {
						switch (var2) {
						case 0:
							Intent var11 = new Intent("WIFI");
							var11.setClass(ActivityBase.this, ActivityLocalPrinters.class);
							ActivityBase.this.startActivityForResult(var11, 2);
							break;
						case 1:
							Intent var9 = new Intent("BT");
							var9.setClass(ActivityBase.this, ActivityLocalPrinters.class);
							ActivityBase.this.startActivityForResult(var9, 2);
							break;
						case 2:
							Intent var7 = new Intent("USB");
							var7.setClass(ActivityBase.this, ActivityLocalPrinters.class);
							ActivityBase.this.startActivityForResult(var7, 2);
							break;
						case 3:
							Intent var5 = new Intent();
							var5.setClass(ActivityBase.this, ActivityCloudPrinters.class);
							ActivityBase.this.startActivityForResult(var5, 2);
							break;
						case 4:
							Intent var3 = new Intent();
							var3.setClass(ActivityBase.this, ActivityPrinters.class);
							ActivityBase.this.startActivityForResult(var3, 2);
						}

						ActivityBase.this.printers_menu.dismiss();
					}
				}).setInverseBackgroundForced(true).create();
	}

	public boolean onCreateOptionsMenu(Menu var1) {
		var1.add(0, 3, 0, 2131099651).setIcon(2130837565);
		return super.onCreateOptionsMenu(var1);
	}

	public boolean onMenuItemSelected(int var1, MenuItem var2) {
		if (var2.getItemId() == 3) {
			this.printers_menu.show();
			return true;
		} else {
			return super.onMenuItemSelected(var1, var2);
		}
	}

	protected void update() {
		Printer var1 = printer;
		ImageView var2 = (ImageView) this.findViewById(2131230760);
		int var3;
		if (var1 != null && var1.online) {
			var3 = 2130837561;
		} else {
			var3 = 2130837560;
		}

		var2.setBackgroundResource(var3);
		TextView var4 = (TextView) this.findViewById(2131230758);
		String var5 = this.getResources().getString(2131099750);
		if (var1 != null) {
			if (var1.title != null) {
				var5 = var1.title;
			} else {
				var5 = "";
			}
		}

		var4.setText(var5);
		TextView var6 = (TextView) this.findViewById(2131230800);
		String var7 = "";
		if (var1 != null) {
			var7 = "[" + var1.owner.login + "]";
			if (var1.owner.nick != null && var1.owner.nick.length() > 0) {
				var7 = var1.owner.nick;
			}

			if (var1.owner.name != null && var1.owner.name.length() > 0) {
				var7 = var1.owner.name;
			}
		}

		var6.setText(var7);
		TextView var8 = (TextView) this.findViewById(2131230759);
		String var9 = "";
		if (var1 != null) {
			if (var1.owner.country != null && var1.owner.country.length() > 0) {
				var9 = var1.owner.country;
			}

			if (var1.owner.city != null && var1.owner.city.length() > 0) {
				StringBuilder var13 = (new StringBuilder()).append(var1.owner.city);
				String var14;
				if (var9.length() > 0) {
					var14 = ", ";
				} else {
					var14 = "";
				}

				var9 = var13.append(var14).append(var9).toString();
			} else if (var1.owner.state != null && var1.owner.state.length() > 0) {
				StringBuilder var11 = (new StringBuilder()).append(var1.owner.state);
				String var12;
				if (var9.length() > 0) {
					var12 = ", ";
				} else {
					var12 = "";
				}

				var9 = var11.append(var12).append(var9).toString();
			}

			if (var1.location != null && var1.location.length() > 0) {
				var9 = var1.location;
			}

			if (var9 == null) {
				var9 = "";
			}
		}

		var8.setText(var9);
		byte var10;
		if (var9.length() > 0) {
			var10 = 0;
		} else {
			var10 = 8;
		}

		var8.setVisibility(var10);
		this.findViewById(2131230801).setVisibility(4);
	}
}
